/*********************************************************************
 *
 *                  Helper Function Defs for Microchip TCP/IP Stack
 *
 *********************************************************************
 * FileName:        Helpers.h
 * Dependencies:    stacktsk.h
 * Processor:       PIC18
 * Complier:        MCC18 v1.00.50 or higher
 *                  HITECH PICC-18 V8.10PL1 or higher
 * Company:         Microchip Technology, Inc.
 *
 * Software License Agreement
 *
 * This software is owned by Microchip Technology Inc. ("Microchip") 
 * and is supplied to you for use exclusively as described in the 
 * associated software agreement.  This software is protected by 
 * software and other intellectual property laws.  Any use in 
 * violation of the software license may subject the user to criminal 
 * sanctions as well as civil liability.  Copyright 2006 Microchip
 * Technology Inc.  All rights reserved.
 *
 * This software is provided "AS IS."  MICROCHIP DISCLAIMS ALL 
 * WARRANTIES, EXPRESS, IMPLIED, STATUTORY OR OTHERWISE, NOT LIMITED 
 * TO MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND 
 * INFRINGEMENT.  Microchip shall in no event be liable for special, 
 * incidental, or consequential damages.
 *
 * Author               Date    Comment
 *~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 * Nilesh Rajbharti     5/17/01 Original        (Rev 1.0)
 * Nilesh Rajbharti     2/9/02  Cleanup
 ********************************************************************/

#ifndef HELPERS_H
#define HELPERS_H

#include "Def_Type.h"



typedef union _WORD_VAL
{
    WORD Val;
    struct
    {
        BYTE LSB_W;
        BYTE MSB_W;
    } byte;
    BYTE v[2];
} WORD_VAL;

#define LSB_W(a)          ((a).v[0])
#define MSB_W(a)          ((a).v[1])

typedef union _DWORD_VAL
{
    DWORD Val;
    struct
    {
        BYTE LOLSB;
        BYTE LOMSB;
        BYTE HILSB;
        BYTE HIMSB;
    } byte;
    struct
    {
        WORD LSW;
        WORD MSW;
    } word;
    BYTE v[4];
} DWORD_VAL;

#define LOWER_LSB(a)    ((a).v[0])
#define LOWER_MSB(a)    ((a).v[1])
#define UPPER_LSB(a)    ((a).v[2])
#define UPPER_MSB(a)    ((a).v[3])

typedef enum LOOP_BUF_STATUS
{
	BUF_NA = ( unsigned char )-1,
    BUF_EMPTY = ( unsigned char )0,
    BUF_FULL = ( unsigned char )1,
	BUF_AVAILABLE = ( unsigned char )2,
}LOOP_BUF_STATUS_ENUM;

typedef struct LOOP_BUF
{
    LOOP_BUF_STATUS_ENUM Status;
    char* InPtr;
    char* OutPtr;
    char* StartAdd;
    char* EndAdd;
    unsigned short Size;
	unsigned short step;
}LOOP_BUF_STRU;

typedef struct LOOP_BUF16
{
    LOOP_BUF_STATUS_ENUM Status;
    unsigned short* InPtr;
    unsigned short* OutPtr;
    unsigned short* StartAdd;
    unsigned short* EndAdd;
    unsigned short Size;
	unsigned short step;
}LOOP_BUF16_STRU;

extern BYTE btohexa_high(BYTE b);
extern BYTE btohexa_low(BYTE b);
extern void Delay_100ns(unsigned char time );
extern unsigned char ExtractField( const char* pStr, char assgin_identifier,char end_delimiter, const char* field_name,char* field_content );
extern BYTE hexatob(WORD_VAL AsciiChars);
extern void LoopBuf_init( LOOP_BUF_STRU* p_LoopBuf ,char* start_add,/* char* end_add, */unsigned short buf_size);
extern char *MemLookupMem(char*s1,unsigned short int n1,char *s2,unsigned short int n2);
extern void *mymemcpy(void * d1, const void * s1, register unsigned char n);
extern LOOP_BUF_STATUS_ENUM PutByteLoopBuf( LOOP_BUF_STRU* p_LoopBuf ,char in_byte );
extern unsigned short CheckLoopBuf_Len(LOOP_BUF_STRU* p_LoopBuf);
extern unsigned short ReadLoopBuf( LOOP_BUF_STRU* p_LoopBuf ,char* out_buf,unsigned short len );
extern unsigned short ReadRxToCh( LOOP_BUF_STRU* p_LoopBuf ,char ch,char* out_buf,unsigned short len);
extern void ResetBuf( LOOP_BUF_STRU* p_LoopBuf);
extern char *strupr (char *s);
extern unsigned short WriteLoopBuf( LOOP_BUF_STRU* p_LoopBuf ,char* in_buf,unsigned short len );
//extern void Delay_us(int microseconds);
//extern void Delay_ms(int milliseconds);

#endif
